package com.xmqian.app.project.ui

import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.coszero.common.config.AppControl
import com.coszero.common.config.Constant
import com.coszero.common.utils.glide.ImageLoad
import com.coszero.common.utils.permission.PermissionHelper
import com.coszero.common.utils.permission.PermissionHelper.CallBackFail
import com.coszero.ui.dialog.ToastUtils
import com.coszero.utils.utils.LogX
import com.coszero.utils.utils.SharedPreUtils
import com.example.xmqian.R
import com.example.xmqian.databinding.ActivitySplashBinding
import com.xmqian.app.project.common.apiadapter.DisplayCutoutUtils
import com.xmqian.app.project.ui.main.MainActivity
import com.xmqian.app.project.ui.preview.activity.TestMainActivity
import com.xmqian.app.project.ui.user.activity.LoginActivity

/**
 * @author xmqian
 * @date 2018/12/27 17:46
 * @desc 过渡界面，可以获取App状态值，判断是否直接进入应用
 */
class SplashActivity : AppCompatActivity() {
    var activitySplashBinding: ActivitySplashBinding? = null
    private var times = 3 //倒计时时间
    private var jumpType = 0 //获取app的状态值，判断跳转哪个界面
    private val handler: Handler = object : Handler(Looper.myLooper()!!) {
        override fun handleMessage(msg: Message) {
            LogX.i("####过渡界面跳转延迟" + msg.what + "秒")
            if (msg.what != 0) {
                activitySplashBinding!!.tvCountdown.text = "跳过" + msg.what + "s"
                times--
                sendEmptyMessageDelayed(times, 1000)
            } else {
                goActivity()
            }
            super.handleMessage(msg)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activitySplashBinding = ActivitySplashBinding.inflate(
            layoutInflater
        )
        setContentView(activitySplashBinding!!.root)
        DisplayCutoutUtils.newInstance(this).setDisplayCutouMode(DisplayCutoutUtils.SHORT_EDGES)
        jumpType = SharedPreUtils.getIntegerData(
            this,
            Constant.JUMP_TYPE,
            if (AppControl.INTO_GUIDE) 0 else 1
        )
        checkPermission()
    }

    private fun checkPermission() {
        PermissionHelper.requestEasy(this, object : CallBackFail {
            override fun requestSuccess() {
                if (AppControl.DELAY_SPLASH) {
                    initView()
                } else {
                    goActivity()
                }
            }

            override fun requestFail() {
                ToastUtils.showMsg("拒绝授予权限会影响应用部分功能的使用")
            }
        })
    }

    private fun initView() {
        activitySplashBinding!!.tvCountdown.setOnClickListener { v: View? ->
            handler.removeMessages(times)
            goActivity()
        }
        //过渡界面是否延迟
        showData()
    }

    /**
     * 跳转界面，根据获取的值判断进入哪个界面
     */
    private fun goActivity() {
        val intent = Intent()
        when (jumpType) {
            0 -> intent.setClass(this, GuideActivity::class.java)
            1 -> if (AppControl.NEED_FRIST_LOGIN) {
                intent.setClass(this, LoginActivity::class.java)
            } else {
                //普通主页
//                    intent.setClass(this, MainActivity.class);
                //底部菜单栏的主页
//                    intent.setClass(this, MainBottomTabActivity.class);
                //测试主页
                intent.setClass(this, TestMainActivity::class.java)
            }

            2 -> intent.setClass(this, MainActivity::class.java)
            else -> {}
        }
        startActivity(intent)
        finish()
    }

    override fun onDestroy() {
        if (handler != null) {
            handler.removeCallbacksAndMessages(null)
        }
        super.onDestroy()
    }

    /**
     * 过渡图片需要动态获取，使用此方法
     */
    @Suppress("deprecation")
    fun showData() {
        //执行闪屏
        ImageLoad.loadImage(
            this,  /*Global.SPLASH_IMAGE_URL*/
            R.drawable.splash_image,
            activitySplashBinding!!.ivSplash,
            object : RequestListener<Drawable?> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any,
                    target: Target<Drawable?>,
                    isFirstResource: Boolean
                ): Boolean {
                    LogX.e(
                        "####SplashActivity",
                        "过渡界面网络图片加载失败,请检查网络或链接地址是否有误"
                    )
                    goActivity()
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any,
                    target: Target<Drawable?>,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    //图片加载成功
                    activitySplashBinding!!.tvCountdown.visibility = View.VISIBLE
                    activitySplashBinding!!.tvCountdown.text = "跳过" + times + "s"
                    handler.sendEmptyMessage(times)
                    return false
                }
            })
    }
}
